package com.elong.businesstravel.protocol.city;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.elong.businesstravel.entitys.AllCityPoi;
import com.elong.businesstravel.entitys.CityPoi;
import com.elong.businesstravel.entitys.Poi;

import android.content.Context;

public class GetPoiByCityAnalyzeJson {

	public interface IPoiByCityAnalyze {
		public void onSucceed(AllCityPoi allCityPoi);
	}

	private ExecutorService executorService = Executors.newFixedThreadPool(1);

	private AllCityPoi mAllCityPoi = new AllCityPoi();

	public GetPoiByCityAnalyzeJson() {
	}
	
	/**
	 * 异步的
	 * 
	 * @param context
	 * @param iPoiByCityAnalyze
	 */
	public GetPoiByCityAnalyzeJson(final String cityId, final Context context,
			final IPoiByCityAnalyze iPoiByCityAnalyze) {
		executorService.execute(new Runnable() {

			@Override
			public void run() {

				String tempString = null;
				try {
					ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
					InputStream inputStream = context.getAssets().open(
							"json/getallcitypoi.json");
					int len = -1;
					byte[] b = new byte[4096];
					while (-1 != (len = inputStream.read(b))) {
						byteArrayOutputStream.write(b, 0, len);
					}
					byteArrayOutputStream.flush();
					tempString = byteArrayOutputStream.toString();
					inputStream.close();
					byteArrayOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

				try {
					JSONObject jsonObject = new JSONObject(tempString);
					JSONObject responseJsonObject = jsonObject
							.optJSONObject("response");
					mAllCityPoi.cityId = cityId;
					JSONObject cityPoiJsonObject = responseJsonObject
							.optJSONObject(cityId);
					if (null != cityPoiJsonObject) {
						analyzeCityPoi(cityPoiJsonObject);
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}
				if (null != iPoiByCityAnalyze) {
					iPoiByCityAnalyze.onSucceed(mAllCityPoi);
				}
			}
		});
	}

	/**
	 * 异步的
	 * 
	 * @param context
	 * @param iPoiByCityAnalyze
	 * @return
	 */
	public void GetPoiByCity(final String cityId, final Context context,
			final IPoiByCityAnalyze iPoiByCityAnalyze) {
		executorService.execute(new Runnable() {

			@Override
			public void run() {

				String tempString = "{}";
				try {

					ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
					InputStream inputStream = context.getAssets().open(
							"citypoi/"+cityId + ".json");
					int len = -1;
					byte[] b = new byte[4096];
					while (-1 != (len = inputStream.read(b))) {
						byteArrayOutputStream.write(b, 0, len);
					}
					byteArrayOutputStream.flush();
					tempString = byteArrayOutputStream.toString();
					inputStream.close();
					byteArrayOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

				try {
					JSONObject jsonObject = new JSONObject(tempString);
					if (null != jsonObject) {
						analyzeCityPoi(jsonObject);
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}
				if (null != iPoiByCityAnalyze) {
					iPoiByCityAnalyze.onSucceed(mAllCityPoi);
				}
			}
		});
	}

	private void analyzeCityPoi(JSONObject cityPoiJsonObject)
			throws JSONException {

		JSONArray tindexJsonArray = cityPoiJsonObject.optJSONArray("tindex");
		if (null != tindexJsonArray) {
			for (int i = 0; i < tindexJsonArray.length(); i++) {
				mAllCityPoi.tindex.add(tindexJsonArray.optString(i));
			}
		}

		for (int i = 0; i < mAllCityPoi.tindex.size(); i++) {
			CityPoi cityPoi = new CityPoi();
			cityPoi.name = mAllCityPoi.tindex.get(i);
			if ("地铁".equals(cityPoi.name)) {
				JSONObject itemCityPoiJsonObject = cityPoiJsonObject
						.optJSONObject(mAllCityPoi.tindex.get(i));
				if (null != itemCityPoiJsonObject) {
					Iterator<String> iterator = itemCityPoiJsonObject.keys();
					while (iterator.hasNext()) {
						String subwayKey = iterator.next();
						CityPoi subwayCityPoi = new CityPoi();
						subwayCityPoi.name = subwayKey;
						JSONArray subwayJsonArray = itemCityPoiJsonObject
								.optJSONArray(subwayKey);
						if (null != subwayJsonArray) {
							for (int j = 0; j < subwayJsonArray.length(); j++) {
								Poi poi = new Poi();
								JSONObject itemPoiObject = subwayJsonArray
										.optJSONObject(j);
								poi.googlelat = itemPoiObject.optDouble(
										"googlelat", 0);
								poi.line = itemPoiObject.optString("line", "");
								poi.poiid = itemPoiObject
										.optString("poiid", "");
								poi.precount = itemPoiObject.optString(
										"precount", "");
								poi.googlelng = itemPoiObject.optDouble(
										"googlelng", 0);
								poi.name = itemPoiObject.optString("name", "");
								poi.cityid = itemPoiObject.optString("cityid",
										"");
								poi.id = itemPoiObject.optString("id", "");
								poi.districtsid = itemPoiObject.optString(
										"districtsid", "");
								poi.districtsname = itemPoiObject.optString(
										"districtsname", "");
								poi.sorts = itemPoiObject
										.optString("sorts", "");
								poi.bussinename = itemPoiObject.optString(
										"bussinename", "");
								poi.bussinesid = itemPoiObject.optString(
										"bussinesid", "");
								subwayCityPoi.pois.add(poi);
							}

						}
						cityPoi.subwayCityPois.add(subwayCityPoi);
					}
				}
			} else {
				JSONArray itemCityPoiJsonArray = cityPoiJsonObject
						.optJSONArray(mAllCityPoi.tindex.get(i));
				for (int j = 0; j < itemCityPoiJsonArray.length(); j++) {
					Poi poi = new Poi();
					JSONObject itemPoiObject = itemCityPoiJsonArray
							.getJSONObject(j);
					poi.googlelat = itemPoiObject.optDouble("googlelat", 0);
					poi.line = itemPoiObject.optString("line", "");
					poi.poiid = itemPoiObject.optString("poiid", "");
					poi.precount = itemPoiObject.optString("precount", "");
					poi.googlelng = itemPoiObject.optDouble("googlelng", 0);
					poi.name = itemPoiObject.optString("name", "");
					poi.cityid = itemPoiObject.optString("cityid", "");
					poi.id = itemPoiObject.optString("id", "");
					poi.districtsid = itemPoiObject
							.optString("districtsid", "");
					poi.districtsname = itemPoiObject.optString(
							"districtsname", "");
					poi.sorts = itemPoiObject.optString("sorts", "");
					poi.bussinename = itemPoiObject
							.optString("bussinename", "");
					poi.bussinesid = itemPoiObject.optString("bussinesid", "");
					cityPoi.pois.add(poi);
				}
			}
			mAllCityPoi.cityPoiList.add(cityPoi);
		}

	}

}
